package udpSample;

import java.net.*;
import java.io.*;

public abstract class UDPServeur extends Thread {
	private int bufferSize;
	protected DatagramSocket sock;
	
	public UDPServeur(int port, int bufferSize)	throws SocketException {
		this.bufferSize = bufferSize;
		this.sock = new DatagramSocket(port);
	}
	
	public UDPServeur(int port) throws SocketException {
		this(port, 8192);
	}
	
	public void run() {
		byte[] buffer = new byte[bufferSize];
		while (true) {
			DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
			try {
				sock.receive(incoming);
				this.respond(incoming);
			}
			catch (IOException e) {
				System.err.println(e);
			}
		} // end while
	}
	public abstract void respond(DatagramPacket request);
}